﻿<%@ WebHandler Language="C#" Class="General" %>
using System;
using System.Web;
using System.Text;
using System.Data;
using System.Collections.Generic;
using System.Web.SessionState;
//using NLog;
using TrackMoney;
public class General : IHttpHandler, IRequiresSessionState
{
    
    //private static Logger logger = LogManager.GetCurrentClassLogger();
    public void ProcessRequest (HttpContext context) 
    {
        //Session protection, if expired, go back login portal
        if (context.Session["id"] == null)
        {
            context.Response.Redirect("Login/Login.htm");
            return;
        }
        int sid = int.Parse(context.Session["ID"].ToString());
        if (context.Request["mode"] != null)
        {
            int pagesize = Convert.ToInt32(context.Request["rows"]);//current line number
            int pagenum = Convert.ToInt32(context.Request["page"]);//current page
            string mode =  context.Request["mode"].ToString();
            switch(mode)
            {
                    //add a new category
                case "AddCategory":
                    AddCategory(context, sid);    
                    break;
                   //retrieve all categories
                case "GetCategories":
                    GetCategories(context, sid);
                    break;
                    //delete a record: category, income or expense
                case "DelRecords":
                    DelRecords(context);
                    break;
                    //add a new income
                case "AddIncome":
                case "AddExpense":
                    AddIncome(context, sid);
                    break;
                case "GetRecords":
                    GetRecords(context, sid);
                    break;
                    //get total
                case "GetCategoryIncome":
                    GetCategorySummary(context, G.eCategory.eCaIncome, sid);
                    break;
                case "GetCategoryExpense":
                    GetCategorySummary(context, G.eCategory.eCaExpense, sid);
                    break;
            }
        }
    }
    private void GetCategorySummary(HttpContext context, G.eCategory type, int sid)
    {
        string period = context.Request["iPeriod"].ToString();
        int iPeriod = (period == "") ? 0 : int.Parse(period);
        DateTime start = DateTime.Now, end = DateTime.Now;
        G.GetPeriod(ref start, ref end, iPeriod);
        context.Response.Write(OpCategory.GetSummary(start, end, type, sid));
    }
    /// <summary>
    /// Delete a record: category, income or expense
    /// </summary>
    /// <param name="context"></param>
    private void DelRecords(HttpContext context)
    {
        string record = context.Request["record"].ToString();
        //remove the last character
        record = record.Substring(0, record.Length - 1);
        string sCurrentStep = context.Request["type"].ToString();
        if (record != "")
        {
            string[] ids = record.Split('|');
            bool bRet = OpCategory.DeleteRecord(ids, sCurrentStep);
            context.Response.Write(bRet.ToString());
        }
        else
        {
            context.Response.Write("False");
        }
    }
    /// <summary>
    /// Get all records: income & expense
    /// </summary>
    /// <param name="context"></param>
    private void GetRecords(HttpContext context, int sid)
    {
        string s = context.Request["start"];
        int start = (s == null ? 0 : int.Parse(s));
        s = context.Request["end"];
        int end = (s == null ? 0 : int.Parse(s));
        s = context.Request["type"];
        string step = (s == null ? "" : s);
        string period = context.Request["iPeriod"];
        //-1 means retrieving all data
        int iPerid = (period == null)?-1:int.Parse(period);
        StringBuilder sb = OpIncome.ConvertIncomeToJson(start, end, step, iPerid, sid);
        context.Response.Write(sb.ToString());
    }
    /// <summary>
    /// Load all categories
    /// </summary>
    /// <param name="context"></param>
    private void GetCategories(HttpContext context, int sid)
    {
        string s = context.Request["start"];
        int start = (s=="" ? 0: int.Parse(s));
        s = context.Request["end"];
        int end = (s == "" ? 0 : int.Parse(s));
        s = context.Request["type"];
        int type = (s == "" ? 0 : int.Parse(s));
        StringBuilder sb = OpCategory.ConvertCategoryToJson(start, end, type, sid);
        context.Response.Write(sb.ToString());
    }
    /// <summary>
    /// add a new income
    /// </summary>
    /// <param name="context"></param>
    private void AddIncome(HttpContext context, int sid)
    {
        string record = context.Request["record"];
        string step = context.Request["mode"];
        bool result = OpIncome.AddIncome(record, step, sid);
        context.Response.Write(result.ToString());
    }
    /// <summary>
    /// Add category
    /// </summary>
    /// <param name="context"></param>
    private void AddCategory(HttpContext context, int sid)
    {
        string record = context.Request["record"];
        bool result = OpCategory.AddCategory(record, sid);
        context.Response.Write(result.ToString());
    }
    public bool IsReusable
    {
        get
        {
            return false;
        }
    }

}